package com.kikidao.app.testclasses.useraccounts;

import com.container.app.annotations.DAOComponent;
import com.container.app.annotations.Inject;
import com.datalayer.orm.ORM;
import com.kikidao.app.exceptions.DAOCheckedExeption;
import org.antlr.stringtemplate.StringTemplate;

import java.sql.SQLException;
import java.util.List;

/**
 * Created with IntelliJ IDEA.
 * User: Georgiana.Scarlat
 * Date: 7/23/12
 * Time: 4:30 PM
 */
@SuppressWarnings("UnusedDeclaration")
@DAOComponent
public class ORMUserDAO implements UserDAO {

    private ORM orm;

    private final static String className = User.class.getCanonicalName();
    private final static String listQuery = "select * from " + className;
    private final static StringTemplate readQuery = new StringTemplate("select * from " + className + " U where U.id = $id$");
    private final static StringTemplate updateQuery = new StringTemplate
            ("update " + className + " U set " +
                    "U.name = '$name$' , U.password = '$password$' , U.age = $age$ where U.id = $id$");


    @Inject
    public void setOrm(ORM orm) {
        this.orm = orm;
    }


    public void create(User transferObject) throws DAOCheckedExeption {
        try {

            orm.createTableIfDoesNotExist(User.class);
            orm.insertObject2DB(transferObject);

        } catch (SQLException e) {
            throw new DAOCheckedExeption("Can't insert user " + transferObject + " into database. ", e);
        }
    }

    public User read(User likeTransferObject) throws DAOCheckedExeption {

        readQuery.reset();
        readQuery.setAttribute("id", likeTransferObject.getId());

        List results = orm.makeQuery(readQuery.toString()).getQueryResults();
        if (results.size() != 1)
            throw new DAOCheckedExeption("There is no user with id " + likeTransferObject.getId() + " in database");
        return (User) results.get(0);

    }

    public void update(User transferObject) throws DAOCheckedExeption {

        updateQuery.reset();
        updateQuery.setAttribute("id", transferObject.getId());
        updateQuery.setAttribute("name", transferObject.getName());
        updateQuery.setAttribute("password", transferObject.getPassword());
        updateQuery.setAttribute("age", transferObject.getAge());

        try {

            orm.makeQuery(updateQuery.toString()).makeUpdate();

        } catch (SQLException e) {
            throw new DAOCheckedExeption("Can't update user " + transferObject, e);
        }

    }

    public void delete(User transferObject) throws DAOCheckedExeption {

        try {

            orm.removeObjectFromDB(transferObject);

        } catch (SQLException e) {
            throw new DAOCheckedExeption("Can't remove user " + transferObject + " from database. ", e);
        }
    }


    public List<User> list() throws DAOCheckedExeption {

        try {

            orm.createTableIfDoesNotExist(User.class);
            return orm.makeQuery(listQuery).getQueryHomogeneousResults(User.class);

        } catch (SQLException e) {
            throw new DAOCheckedExeption("Can't read users from the database. ");
        }


    }
}
